﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beeldbankmvc.Models;
using System.Configuration;

namespace Beeldbankmvc.Controllers
{
    public class ProjectFacetController : Controller
    {
        ProjectRepository projectRepository = new ProjectRepository();

        //
        // GET: /ProjectFacet/
        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Index()
        {
            return View(projectRepository.FindAll());
        }

        //
        // GET: /ProjectFacet/Details/5

        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Details(int id)
        {
            return View(projectRepository.FindOne(id));
        }

        //
        // GET: /ProjectFacet/Create

        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ProjectFacet/Create

        [HttpPost]
        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                BEELDBANK_MEDIAOBJECT_FACETS_PROJECT project = new BEELDBANK_MEDIAOBJECT_FACETS_PROJECT();
                project.Project = collection["Project"];
                project.Publicatie = ConfigurationManager.AppSettings["publicationName"];
                projectRepository.Add(project);
                projectRepository.Save();


                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ProjectFacet/Edit/5

        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Edit(int id)
        {
            return View(projectRepository.FindOne(id));
        }

        //
        // POST: /ProjectFacet/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                BEELDBANK_MEDIAOBJECT_FACETS_PROJECT project = projectRepository.FindOne(id);
                project.Project = collection["Project"];
                project.Publicatie = ConfigurationManager.AppSettings["publicationName"];
                projectRepository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ProjectFacet/Delete/5

        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Delete(int id)
        {
            return View(projectRepository.FindOne(id));
        }

        //
        // POST: /ProjectFacet/Delete/5

        [HttpPost]
        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                BEELDBANK_MEDIAOBJECT_FACETS_PROJECT project = projectRepository.FindOne(id);
                projectRepository.Delete(project);
                projectRepository.Save();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
